package com.itheima.auth.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.support.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException,
            ServletException {
        httpServletResponse.setCharacterEncoding("UTF-8");
        /*
         * 发送其他的异常信息
         * */
        Result<String> result=Result.buildError(e.getMessage());
        ObjectMapper objectMapper=new ObjectMapper();
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));
    }
}
